package com.ushine.web.communication.websocket.config;


import com.ushine.web.communication.websocket.WsContant;
import com.ushine.web.communication.websocket.contrroller.SysWsController;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @Author yls
 * @Date 2020/3/31 10:45
 * @Description webSocket消息队列控制
 * //@EnableWebSocketMessageBroker 开启WebSocket消息代理
 * @Version 1.0
 **/
@Configuration
@EnableWebSocketMessageBroker
@ComponentScan(basePackageClasses = {SysWsController.class})
public class WebSocketStomServer implements WebSocketMessageBrokerConfigurer {


    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        //注册一个Stomp的节点（endpoint）,并指定使用SockJS协议,前端连接地址：http://172.16.1.103:8888/ws/endpointWisely
        stompEndpointRegistry.addEndpoint(WsContant.WEBSOCKET_PATH)
                .addInterceptors(new WsInterceptors())
                .setAllowedOrigins("*")
                .withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        /*
         * 服务端发送消息给客户端的域,多个用逗号隔开
         * WsContant.WEB_SOCKET_BROADCAST_PATH: 广播地址
         * WsContant.P2P_PUSH_BASE_PATH：点对点地址
         */
        registry.enableSimpleBroker(WsContant.WEBSOCKET_BROADCAST_PATH, WsContant.P2P_PUSH_BASE_PATH);
        //定义一对一推送的时候前缀
        registry.setUserDestinationPrefix(WsContant.P2P_PUSH_BASE_PATH);
        //定义websoket前缀,全局使用的消息前缀（客户端订阅路径上会体现出来）
        registry.setApplicationDestinationPrefixes(WsContant.WEBSOCKET_PATH_PREFIX);
    }

}
